import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // 获取当前环境的配置
  const config = loadEnv(mode, './')
  return {
    server: {
      //使用IP能访问
      host: '0.0.0.0',
      // 接口代理
      proxy: {
        '/tinymce-editor': {
          target: config.VITE_IPCONFIG, // 请求得对方地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/tinymce-editor/, ''),
        },
      },
    },
    plugins: [
      vue(),
      legacy({
        targets: ['chrome < 60', 'edge < 15'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: false,
        modernPolyfills: [
          'es.string.replace-all',
          'es.global-this',
          'es.symbol',
          'es.array.filter',
          'es.array.flat-map',
          'es.promise',
          'es.promise.finally',
          'es/map',
          'es/set',
          'es.array.for-each',
          'es.object.define-properties',
          'es.object.define-property',
          'es.object.get-own-property-descriptor',
          'es.object.get-own-property-descriptors',
          'es.object.keys',
          'es.object.to-string',
          'web.dom-collections.for-each',
          'esnext.global-this',
          'esnext.string.match-all',
        ],
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/mixin.scss";',
        },
      },
    },
    build: {
      target: 'es2015',
    },
  }
})
